/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wehealth.three.lib_connect.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class PreferenceUtils {

    private String PREFER_IDCARDNO = "prefer_idcardno";


    /**
     * 保存Preference的name
     */
    public static final String PREFERENCE_NAME = "equipment_saveInfo";
    private static SharedPreferences mSharedPreferences;
    private static PreferenceUtils mPreferenceUtils;
    private static SharedPreferences.Editor editor;

    private String SHARED_KEY_BACKGROUND_STYLE = "shared_key_background_style";
    private String SHARED_KEY_UPDATE_VERSION = "shared_key_update_version";
    private String SHARED_KEY_STYLE = "shared_key_style";
    private String SHARED_KEY_SOUND = "shared_key_sound";
    private String SHARED_KEY_FILTER = "shared_key_filter";
    private String SHARED_KEY_BUTTON_CHECK = "shared_key_button_check_state";
    private String SHARED_KEY_UPLOAD_CHECK = "shared_key_upload_check_count";
    private String SHARED_KEY_PACE = "shared_key_pace";

    private String PREFER_TEMP_SERIALNO = "prefer_temp_serialno";

    private String SHARED_KEY_SAVEFILE_STYLE = "shared_key_savefile_style";
    private String SHARED_KEY_DEVICE_SERIALNO = "shared_key_device_serialno";
    private String SHARED_KEY_DISPALY_STYLE = "shared_key_display_style";
    private String SHARED_KEY_IS_PLAYVIDEO = "shared_key_is_play_video";
    private String PREFER_DEVICE_BLUETOOTH_TYPE = "prefer_device_bluetooth_type";
    private String PREFER_ECGDEVICE_ADDMAC = "prefer_ecgdevice_addmac";
    private String PREFER_VERSION = "prefer_new_version";

    private String PREFER_BLE_DEVICE_ADDMAC = "prefer_ble_device_addmac";

    private String SHARED_KEY_SERVER_HOST = "shared_key_serverhost";

    private PreferenceUtils(Context cxt) {
        mSharedPreferences = cxt.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
    }

    public static synchronized void init(Context cxt) {
        if (mPreferenceUtils == null) {
            mPreferenceUtils = new PreferenceUtils(cxt);
        }
    }

    /**
     * 单例模式，获取instance实例
     */
    public static PreferenceUtils getInstance() {
        if (mPreferenceUtils == null) {
            new RuntimeException("请先初始化");
        }
        return mPreferenceUtils;
    }

    /**
     * 设置网络请求的服务器地址
     */
    public void setServerHost(String host) {
        editor.putString(SHARED_KEY_SERVER_HOST, host);
        editor.commit();
    }

    /**
     * 返回网络请求的服务器地址
     */
    public String getServerHost() {
        return mSharedPreferences.getString(SHARED_KEY_SERVER_HOST, "https://api.5wehealth.com/ECGPlatformService/jaxrs/");
    }

    /**
     * 设置为true,背景风格为深蓝色
     */
    public void setChangeStyle(boolean paramBoolean) {
        editor.putBoolean(SHARED_KEY_BACKGROUND_STYLE, paramBoolean);
        editor.commit();
    }





    /**
     * 设置 保存文件的方式：2为24小时模式；  1为人工Manual； 0为自动 Auto:10秒数据
     */
    public void setSaveFileStyle(int saveFileStyle) {
        editor.putInt(SHARED_KEY_SAVEFILE_STYLE, saveFileStyle);
        editor.commit();
    }

    /**
     * 返回 保存文件的方式：2为24小时模式；  1为人工Manual； 0为自动 Auto:10秒数据
     */
    public int getSaveFileStyle() {
        return mSharedPreferences.getInt(SHARED_KEY_SAVEFILE_STYLE, 0);
    }

    /**
     * 设置为0, 显示起搏;    为1, 有起搏也不显示
     */
    public void setPace(int paramBoolean) {
        editor.putInt(SHARED_KEY_PACE, paramBoolean);
        editor.commit();
    }

    /**
     * 返回为0, 显示起搏;    为1, 有起搏也不显示
     */
    public int getPace() {
        return mSharedPreferences.getInt(SHARED_KEY_PACE, 1);
    }

    /**
     * 设置为0, 6ch*2 模式; 为1, 12ch*1模式；   为2， 1ch*1模式
     */
    public void setDisplayStyle(int paramBoolean) {
        editor.putInt(SHARED_KEY_DISPALY_STYLE, paramBoolean);
        editor.commit();
    }

    /**
     * 返回为0, 6ch*2 模式; 返回为1, 12ch*1模式；   返回为2， 1ch*1模式
     */
    public int getDisplayStyle() {
        return mSharedPreferences.getInt(SHARED_KEY_DISPALY_STYLE, 1);
    }


    //是ble模式为true，否则为false
    public void setDeviceBluetoothType(boolean isBle) {
        editor.putBoolean(PREFER_DEVICE_BLUETOOTH_TYPE, isBle).commit();
    }

    //ble模式返回false；否则返回true
    public boolean getDeviceBluetoothType() {
        return mSharedPreferences.getBoolean(PREFER_DEVICE_BLUETOOTH_TYPE, true);
    }
    public void setVersion(boolean haveVersion) {
        editor.putBoolean(PREFER_VERSION, haveVersion).commit();
    }

    public boolean getVersion() {
        return mSharedPreferences.getBoolean(PREFER_VERSION, false);
    }

    public void setECGDeviceBTMAC(String idCardNo, String ecgDeviceBTMAC) {
        if (TextUtils.isEmpty(ecgDeviceBTMAC)) {
            editor.remove("ECGDEVICE_ADDMAC" + idCardNo).commit();
            return;
        }
        editor.putString("ECGDEVICE_ADDMAC" + idCardNo, ecgDeviceBTMAC).commit();
    }


    public String getECGDeviceBTMAC(String idCardNo) {
        String result = mSharedPreferences.getString("ECGDEVICE_ADDMAC"+idCardNo, "");
        return  result;
    }




    public void setTempSerialNo(String serialNo) {
        editor.putString(PREFER_TEMP_SERIALNO, serialNo).commit();
    }

    public String getTempSerialNo() {
        return mSharedPreferences.getString(PREFER_TEMP_SERIALNO, "");
    }

    public void setIdCardNo(String idCardNo) {
        if (TextUtils.isEmpty(idCardNo)) {
            editor.remove(PREFER_IDCARDNO).commit();
            return;
        }
        editor.putString(PREFER_IDCARDNO, idCardNo).commit();
    }

    public String getIdCardNo() {
        return mSharedPreferences.getString(PREFER_IDCARDNO, "");
    }


    public void setECGDeviceBTMAC(String ecgDeviceBTMAC) {
        editor.putString(PREFER_ECGDEVICE_ADDMAC, ecgDeviceBTMAC).commit();
    }


    public void setBleDeviceMAC(String ecgDeviceBTMAC) {
        editor.putString(PREFER_BLE_DEVICE_ADDMAC, ecgDeviceBTMAC).commit();
    }

    public void setIsAgreeOne(String key, Boolean isAgree) {
        editor.putBoolean(key, isAgree).commit();
    }

    public Boolean getIsAgreeOne(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }


    public void setIsAgreeTwo(String key, Boolean isAgree) {
        editor.putBoolean(key, isAgree).commit();
    }

    public Boolean getIsAgreeTwo(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }

    public String getBleDeviceMAC() {
        return mSharedPreferences.getString(PREFER_BLE_DEVICE_ADDMAC, "");
    }

    /**
     * 返回为true,背景风格为深蓝色
     */
    public boolean getChangeStyle() {
        return mSharedPreferences.getBoolean(SHARED_KEY_BACKGROUND_STYLE, true);
    }

    /**
     * 设置为true, 系统自动升级
     */
    public void setUpdateVersion(boolean paramBoolean) {
        editor.putBoolean(SHARED_KEY_UPDATE_VERSION, paramBoolean);
        editor.commit();
    }

    /**
     * 返回为true, 系统自动升级
     */
    public boolean getUpdateVersion() {
        return mSharedPreferences.getBoolean(SHARED_KEY_UPDATE_VERSION, true);
    }

    /**
     * 设置为true, 演示模式
     */
    public void setStyle(boolean paramBoolean) {
        editor.putBoolean(SHARED_KEY_STYLE, paramBoolean);
        editor.commit();
    }

    /**
     * 返回为true, 演示模式
     */
    public boolean getStyle() {
        return mSharedPreferences.getBoolean(SHARED_KEY_STYLE, false);
    }





}
